package dk.im2b.whack_an_ad;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class TheGameSurface extends SurfaceView implements Runnable {

	SurfaceHolder surfaceHolder;
	List<Bitmap> spriteListe;
	Bitmap background, scaledBackground, hole1;
	Bitmap scaledHole1, scaledHole2, scaledHole3, scaledHole4, scaledHole5, scaledBomb1;
	Bitmap appleLogo, colaLogo, dellLogo, bomb;
	
	Canvas canvas;
	
	Thread thread;

	Point scaleValue = new Point();
	int width;
	int height;
	
	float positionX;
	float positionY;

	public TheGameSurface(Context context, AttributeSet attrs) {
		super(context, attrs);

		Initialize();

		background = BitmapFactory.decodeResource(getResources(),
				R.drawable.background);

		this.surfaceHolder = getHolder();

		height = getHeight();
		width = getWidth();

		thread = new Thread(this);
		thread.start();

	}

	private void Initialize() {
		spriteListe = new ArrayList<Bitmap>();

		hole1 = BitmapFactory.decodeResource(getResources(),
				R.drawable.molehole);

		appleLogo = BitmapFactory.decodeResource(getResources(),
				R.drawable.apple_logo);
		colaLogo = BitmapFactory.decodeResource(getResources(),
				R.drawable.coca_cola_logo);
		dellLogo = BitmapFactory
				.decodeResource(getResources(), R.drawable.dell);
		bomb = BitmapFactory.decodeResource(getResources(), R.drawable.bomb);
		

		// spriteListe.add(Bitmap.createScaledBitmap(appleLogo,
		// appleLogo.getWidth() / scaleValue.x, appleLogo.getHeight()
		// / scaleValue.y, false));
		// spriteListe.add(Bitmap.createScaledBitmap(colaLogo,
		// colaLogo.getWidth()
		// / scaleValue.x, colaLogo.getHeight() / scaleValue.y, false));
		// spriteListe.add(Bitmap.createScaledBitmap(dellLogo,
		// dellLogo.getWidth()
		// / scaleValue.x, dellLogo.getHeight() / scaleValue.y, false));
	}

	private void ScaledSprites() {
		scaledHole1 = Bitmap.createScaledBitmap(hole1, hole1.getWidth()
				/ scaleValue.x, hole1.getHeight() / scaleValue.y, false);

		scaledHole2 = Bitmap.createScaledBitmap(hole1, hole1.getWidth()
				/ scaleValue.x, hole1.getHeight() / scaleValue.y, false);

		scaledHole3 = Bitmap.createScaledBitmap(hole1, hole1.getWidth()
				/ scaleValue.x, hole1.getHeight() / scaleValue.y, false);

		scaledHole4 = Bitmap.createScaledBitmap(hole1, hole1.getWidth()
				/ scaleValue.x, hole1.getHeight() / scaleValue.y, false);

		scaledHole5 = Bitmap.createScaledBitmap(hole1, hole1.getWidth()
				/ scaleValue.x, hole1.getHeight() / scaleValue.y, false);
		
		scaledBomb1 = Bitmap.createScaledBitmap(bomb, bomb.getWidth() / 
				scaleValue.x, bomb.getHeight() / scaleValue.y, false);
		

	}
	
	public void DrawBomb(final Canvas canvas, float x, float y)
	{
		positionX = x;
		positionY = y;
		
		Handler mHanlder = new Handler();
		mHanlder.post(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				canvas.drawBitmap(scaledBomb1, positionX, positionY, null);
				int animateTime = 0;
				if(animateTime < 10)
				{
					positionY += 10;
					animateTime += 1;
				}
				else
				{
					animateTime = 0;
					
				}
				
				invalidate();
			}
		});
		
		
		
		
		
	}
	
	@Override
	public void run() {
		while (true) {
			if (!this.surfaceHolder.getSurface().isValid())
				continue;
			canvas = surfaceHolder.lockCanvas();
			scaledBackground = Bitmap.createScaledBitmap(background,
					canvas.getWidth(), canvas.getHeight(), false);

			scaleValue.set(canvas.getWidth() / scaledBackground.getWidth(),
					canvas.getHeight() / scaledBackground.getHeight());

			ScaledSprites();

			canvas.drawBitmap(scaledBackground, 0, 0, null);

			canvas.drawBitmap(scaledHole1, (canvas.getWidth() / 5)
					- (scaledHole1.getWidth() / 2),
					(600) - (scaledHole1.getHeight() / 2), null);

			canvas.drawBitmap(scaledHole2,
					(canvas.getWidth() - (canvas.getWidth() / 5))
							- (scaledHole2.getWidth() / 2),
					(600) - (scaledHole2.getHeight() / 2), null);

			canvas.drawBitmap(scaledHole3,
					(canvas.getWidth() - (canvas.getWidth() / 2))
							- (scaledHole3.getWidth() / 2),
					(750) - (scaledHole3.getHeight() / 2), null);

			canvas.drawBitmap(scaledHole4, (canvas.getWidth() / 5)
					- (scaledHole4.getWidth() / 2),
					(900) - (scaledHole4.getHeight() / 2), null);

			canvas.drawBitmap(scaledHole5,
					(canvas.getWidth() - (canvas.getWidth() / 5))
							- (scaledHole5.getWidth() / 2),
					(900) - (scaledHole5.getHeight() / 2), null);
			
			DrawBomb(canvas,(canvas.getWidth() / 5)
					- (scaledHole1.getWidth() / 2), (600) - (scaledHole1.getHeight() / 2));
			
			
			
			this.surfaceHolder.unlockCanvasAndPost(canvas);
		}
	}

}
